package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: dpeletei
 * Date: 14.05.13
 * Time: 16:06
 */
public class Biblioteca {

    private ArrayList<Book> bookList;
    private ArrayList<Movie> movieList;
    private ArrayList<User> userList;
    private int libraryNumber = 1111111;
    private User logedUser;
    private boolean loged;

    public Biblioteca(){
        this.bookList = new ArrayList<Book>();
        this.movieList = new ArrayList<Movie>();
        this.userList = new ArrayList<User>();
        this.loged = false;
        logedUser = null;
    }

    public void addBook(String pName){
        this.bookList.add(new Book(pName));
    }

    public boolean hasBook(String pName){
        for(Book item: this.bookList) {
            if (item.getName().equals(pName)) {
                return true;
            }
        }
        return false;
    }

    public String printBookList(){
        String list= "";
        for(Book item: this.bookList){
            list += item.getName() + "\n";
        }
        return list;
    }

    public void addMovie(String pName, String pYear, String pDirector, String pRating) {
        this.movieList.add(new Movie(pName, pYear, pDirector, pRating));
    }

    public String printMovieList() {
        String list = "";
        for(Movie item: this.movieList){
            list += item.getName() + "\t" + item.getYear() + "\t" + item.getDirector() + "\t" + item.getRating() + "\n";
        }
        return list;
    }

    public void addUser(String pName, String pPhoneNumber, String pEmail, String pPassword) {
        this.userList.add(new User(Integer.toString(libraryNumber), pPassword, pName, pEmail, pPhoneNumber));
        this.updateLibraryNumber();
    }

    private void updateLibraryNumber() {
        libraryNumber++;
     }

    public String printUserList(){
        String list = "";
        for(User item: this.userList){
            list += item.printUserDetails() + "\n";
        }
        return list;
    }

    public boolean hasUser(String pUserName) {
        for(User item: this.userList){
            if(item.getUserName().equals(pUserName)){
                return true;
            }
        }
        return false;
    }

    public boolean logIn(String pUserName, String pPassWord) {
        if(this.hasUser(pUserName)){
            User logUser = this.getUserByUserName(pUserName);
            if(logUser.verifyUserPassword(pUserName,pPassWord)){
                return true;
            }
        }
        return false;
    }

    public User getUserByUserName(String pUserName) {
        for(User item:  this.userList){
            if(item.getUserName().equals(pUserName)){
                return item;
            }
        }
        return null;
    }

    public void setLogedUser(User pUser){
        this.logedUser = pUser;
    }

    public User getLogedUser(){
        return this.logedUser;
    }

    public boolean isLoged(){
        return this.loged;
    }

    public void setLoged(){
        this.loged = true;
    }


}